package com.example.iot.entity.po;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.example.iot.entity.RuleChain;
import com.example.iot.entity.id.RuleChainId;
import com.example.iot.entity.id.RuleNodeId;
import com.example.iot.mapper.mysql.handlers.UUIDTypeHandler;
import com.example.iot.util.UUIDUtil;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Objects;
import java.util.UUID;

/**
 * <p>
 * 
 * </p>
 *
 * @author ChenXiangpeng
 * @since 2021-02-25
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName(value = "rule_chain", autoResultMap = true)
public class RuleChainPO extends BasePO implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * x轴位置
     */
    private String positionX;

    /**
     * y轴位置
     */
    private String positionY;

    /**
     * 配置
     */
    @TableField(typeHandler = JacksonTypeHandler.class)
    private JsonNode configuration;

    /**
     * 规则链名称
     */
    private String name;

    /**
     * 首位节点id
     */
    @TableField(typeHandler = UUIDTypeHandler.class)
    private UUID firstNodeId;

    public static final String ID = "id";

    public static final String POSITION_X = "position_x";

    public static final String POSITION_Y = "position_y";

    public static final String CONFIGURATION = "configuration";

    public static final String NAME = "name";

    public static final String FIRST_NODE_ID = "first_node_id";

    public RuleChain toDto(){
        RuleChain ruleChain = new RuleChain();
        ruleChain.setId(new RuleChainId(UUIDUtil.fromString(this.getUuid())));
        ruleChain.setName(this.getName());
        ruleChain.setFirstRuleNodeId(new RuleNodeId(this.getFirstNodeId()));
        ruleChain.setConfiguration(this.getConfiguration());
        return ruleChain;
    }

    public static RuleChainPO ofDto(RuleChain dto){
        RuleChainPO ruleChainPO = new RuleChainPO();
        ruleChainPO.setConfiguration(dto.getConfiguration());
        ruleChainPO.setFirstNodeId(Objects.isNull(dto.getFirstRuleNodeId()) ? null : dto.getFirstRuleNodeId().getId());
        ruleChainPO.setPositionX(dto.getPositionX());
        ruleChainPO.setPositionY(dto.getPositionY());
        ruleChainPO.setName(dto.getName());
        return ruleChainPO;
    }
}
